

#include <master.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <din.h>




#include "Mxd0250Private.h"
#include "Mxd0250Config.h"

MXD_DEVICE_CONFIG g_MiraDeviceConfig = {MXD_SPI, MXD_SPI, FALSE, 0x30, MXD_SPI_MOTO_M0, 20000, MXD_TS_MSB_MODE, MXD_TS_POSEDGE, MXD_TS_PARALLEL, MXD_TS_30_MODE, 0, MXD_INT_LOW_ACTIVE };

/*!
 * \par Description
 * Set config for this demod chip.
 * Notice: This function must be re-implemented to fit different hardware design.
 *
 * \param nDemodChipIndex   [IN] the chip index which host is operating, the first index is 0; if there is only one chip in your system, just ignore it.
 *
 * \return
 *    MXD_DEVICE_CONFIG - some config of hardware design, include mpeg2ts interface and I2C interface.
 *
 * \warning
 */
MXD_DEVICE_CONFIG* MxdPorting_GetDeviceConfig(MXD_UINT8 nDemodChipIndex)
{
    return &g_MiraDeviceConfig;
}

/*!
 * \par Description
 * Write one data to a I2C register of demod chip in host environment.
 * Notice: MIRA is as slave in I2C communication and MIRA slave address is ?; This function must be re-implemented to fit different hardware design.
 *
 * \param nDemodChipIndex   [IN] the chip index which host is operating, the first index is 0; if there is only one chip in your system, just ignore it.
 * \param addr  [IN] the Register address;
 * \param data  [IN] the register data;
 *
 * \return
 *    MXD_RTN_OK    -   the call succeeded
 *
 * \warning
 */
MXD_RETURN_CODE MxdPorting_I2C_Write(MXD_UINT8 nDemodChipIndex, MXD_UINT8 slaveAddr, MXD_UINT8 addr, MXD_UINT8* data)
{
    return MXD_RTN_OK;
}

/*!
 * \par Description
 * Read one byte data from a I2C register of demod chip in host environment.
 * Notice: MIRA is as slave in I2C communication and MIRA slave address is ?; This function must be re-implemented to fit different hardware design.
 *
 * \param nDemodChipIndex   [IN] the chip index which host is operating, the first index is 0; if there is only one chip in your system, just ignore it.
 * \param addr  [IN] the Register address;
 * \param data  [OUT] the register data;
 *
 * \return
 *    MXD_RTN_OK    -   the call succeeded
 *
 * \warning
 */
MXD_RETURN_CODE MxdPorting_I2C_Read(MXD_UINT8 nDemodChipIndex, MXD_UINT8 slaveAddr, MXD_UINT8 addr, MXD_UINT8 *data)
{
    return MXD_RTN_OK;
}

extern int          SMS_SPI_Read_Bytes(unsigned char* buffer, int len);
extern int          SMS_SPI_Write_Bytes(unsigned char* buffer, int len);
/*!
 * \par Description
 * Transmit and receive some SPI data in host environment.
 * Notice: This function must be re-implemented to fit different hardware design.
 *
 * \param nDemodChipIndex   [IN] the chip index which host is operating, the first index is 0; if there is only one chip in your system, just ignore it.
 * \param trxLen    [IN] the length of data will be transmited and received;
 * \param pDataTx   [IN] the transmiting data;
 * \param pDataRx   [IN] the received data;
 *
 * \return
 *    MXD_RTN_OK    -   the call succeeded
 *
 * \warning
 */
extern void tcmmbdrv_SetCsState(int nLevel);
MXD_RETURN_CODE MxdPorting_SPI_TxRx(MXD_UINT8 nDemodChipIndex, MXD_UINT32 trxLen, MXD_UINT8* pDataTx,  MXD_UINT32 revLen, MXD_UINT8* pDataRx)
{
    // int i;


    //while(1)
    //{
    //tcmmbdrv_SetCsState(0);
    //for( i=0; i<trxLen; i++ )
    {
        SMS_SPI_Write_Bytes(pDataTx, trxLen);
        SMS_SPI_Read_Bytes(pDataRx + trxLen, revLen - trxLen);
    }
    //tcmmbdrv_SetCsState(1);

    //if( revLen > 4000 )
	//CMMB_Delay(5);

    //}
    return MXD_RTN_OK;
}

#if 0
extern unsigned char ucSPI_Init_Flag;
void MXD_Porting_HardReset()
{
	ucSPI_Init_Flag = 1; 
	InnofideiPowerOff();
    tcmmbdrv_SetCsState(1);
    INNO_SetSPI_Master_DMA_Mode(2);
    InnofideiPowerOnAndReset();
    tcmmbdrv_SetCsState(0);
    //return MXD_RTN_OK;
}
#endif

